<?php

function getSubitems($domain) {
    
    $subItems = array();
    
    $rights = $domain::getItems();
    foreach($rights as $id) {
        if (isset(Constants::$rights[$id]['url'])) {
            $subItems[] = Constants::$rights[$id];
        }
    }
    
    return $subItems;
}
?>

<tr>
<td>
    <div class="middle_zone_menu">
        <ul class="main_menu_list">
            
            <li class='main_menu_item float_left <?php echo ($controllerDomain == 'dashboard')? 'selected' : '';?>'><a href='<?php echo $this->Html->url(array('controller' => 'dashboard', 'action' => 'index')); ?>'><span>Dashboard</span><div class="hidden"><ul></ul></div></a></li>
            <?php 
            foreach (Constants::$domains as $domain) {
                
                if ($domain::$title == 'Application') {
                    continue;
                }
                
                $subItems = getSubItems($domain);
                $selected = ($controllerDomain == $domain::$id)? 'selected' : '';
                
                if (count($subItems) > 0 ) {
                    echo "<li class='main_menu_item float_left {$selected}'><span>{$domain::$title}</span>";
                    echo "<div class='hidden'><ul>";
                    foreach ($subItems as $subItem) {
                        echo "<li class='main_submenu_item float_left'><a href='{$this->Html->url($subItem['url'])}'><span>{$subItem['name']}</span></a></li>";
                    }
                    echo "</ul></div></li>";
                }
            }
            ?>
        </ul>
    </div>
</td>
</tr>
<tr>
<td>
    <div class="middle_zone_submenu">
        <ul class="main_submenu_list">
        </ul>
    </div>
</td>
</tr>

<script language="Javascript">
    $(document).ready(function() {
        
        $('.main_submenu_list').html($('.selected div ul').html());
        
        $('.main_menu_item').click(function(event){
            $('.selected').removeClass('selected');
            $(event.currentTarget).addClass('selected');
            
            var content = $(event.currentTarget).children('div').children('ul').html();
            $('.main_submenu_list').html(content);
        });
    });
</script>

